package cn.luoshaocheng.system.controller;

import cn.luoshaocheng.basic.service.IPermissionScanService;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.system.domain.Menu;
import cn.luoshaocheng.system.domain.Permission;
import cn.luoshaocheng.system.query.MenuQuery;
import cn.luoshaocheng.system.query.PermissionQuery;
import cn.luoshaocheng.system.service.IMenuService;
import cn.luoshaocheng.system.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService iPermissionService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Permission> loadAll() {
        return iPermissionService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Permission loadById(@PathVariable("id") Long id) {
        return iPermissionService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public jsonResult deleteById(@PathVariable("id") Long id) {
        try {
            iPermissionService.delete(id);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param
     * @return
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody Permission permission) {
        try {
            if (permission.getId() == null) {
                iPermissionService.save(permission);
            } else {
                iPermissionService.update(permission);
            }
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  jsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param
     * @return
     */
    @PostMapping
    public PageList<Permission> queryPage(@RequestBody PermissionQuery permissionQuery) {
        return iPermissionService.queryPage(permissionQuery);

    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            iPermissionService.patchDel(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  jsonResult.me().setMsg("删除失败");
        }
    }

}

